package com.jeesite.modules.examine.web;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.car.entity.Car;
import com.jeesite.modules.car.service.CarService;
import com.jeesite.modules.general.CosUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.examine.entity.Examine;
import com.jeesite.modules.examine.service.ExamineService;

/**
 * 审核记录Controller
 * @author Qu
 * @version 2023-12-20
 */
@Controller
@RequestMapping(value = "${adminPath}/examine/examine")
public class ExamineController extends BaseController {

	@Autowired
	private ExamineService examineService;
    @Autowired
    private CarService carService;

	DecimalFormat df = new DecimalFormat("0.00");
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("examine:examine:view")
	@RequestMapping(value = {"list", ""})
	public String list(Examine examine, Model model) {
		model.addAttribute("examine", examine);
		return "modules/examine/examineList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("examine:examine:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Examine> listData(Examine examine, HttpServletRequest request, HttpServletResponse response) {
		examine.setPage(new Page<>(request, response));
		Page<Examine> page = examineService.findPage(examine);
		return page;
	}

	/**
	 * 审核信息
	 */
	@RequestMapping(value = "examineInfo")
	public String examineInfo(Car car,Examine examine, Model model) {
		JSONArray array1 = new JSONArray();
		JSONArray array2 = new JSONArray();
		List<Car> list = carService.findImageList(car.getCarId());
		for(int i=0;i<list.size();i++){
			if("0".equals(list.get(i).getImageType())){
				array1.add(CosUtil.URL+list.get(i).getImageUrl());
			}
			if("1".equals(list.get(i).getImageType())){
				array2.add(CosUtil.URL+list.get(i).getImageUrl());
			}
		}
		Car cars = carService.get(car);
		cars.setRemarks(StringUtils.isEmpty(cars.getRemarks())?"无":cars.getRemarks());
		cars.setShowDate(DateUtils.formatDate(cars.getRegisterDate(),"yyyy年MM月"));
		cars.setShowSellPrice(df.format(cars.getSellPrice()/100));
		cars.setCoverUrl(CosUtil.URL+cars.getCoverUrl());
		model.addAttribute("carList", array1);
		model.addAttribute("detailList", array2);
		model.addAttribute("examine", examine);
		model.addAttribute("car", cars);
		return "modules/examine/examineInfo";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("examine:examine:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Examine examine) {
		Map map = new HashMap();
		map.put("carId",examine.getCarId());
		map.put("carStatus",examine.getExamineResult());
		carService.updateCarStatus(map);
		examineService.save(examine);
		return renderResult(Global.TRUE, text("保存成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("examine:examine:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Examine examine) {
		examineService.delete(examine);
		return renderResult(Global.TRUE, text("删除成功！"));
	}
	
}